﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace DotNet.Utilities.MD5
{
    public class MD5
    {
        public string getHash(string sText)
        {
            byte[] aText = Encoding.UTF8.GetBytes(sText.Trim());
            MemoryStream ms = new MemoryStream(aText);
            return getHash(ms);
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="oStream"></param>
        /// <returns></returns>
        public string getHash(Stream oStream)
        {
            oStream.Seek(0, SeekOrigin.Begin);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] aHash = md5.ComputeHash(oStream);
            StringBuilder oSb = new StringBuilder();
            for (int i = 0; i < aHash.Length; i++)
            {
                oSb.Append(aHash[i].ToString("x2"));
            }
            return oSb.ToString().ToLower();
        }
    }
}
